package com.oms.app.address.action;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oms.app.address.model.Address;
import com.oms.app.address.service.IAddressService;
import com.oms.app.sequence.service.impl.SequenceManager;
import com.oms.base.OMSConfigHolder;
import com.oms.util.OMSFileUtils;
/**
 * 地址表
 * controller
 * @author wtj
 * @date Sep 23, 2012  23:18:26 PM
 */
@RequestMapping("")
public class AddressAction {
	private String webRoot = OMSConfigHolder.getWebRoot();
	String fileRoot = webRoot+"jsp/manager/address/";
	private Map<String, Object> map = new HashMap<String, Object>();
	private IAddressService addressService;

	public IAddressService getAddressService() {
		return addressService;
	}

	public void setAddressService(IAddressService addressService) {
		this.addressService = addressService;
	}
//######################  tree 树 #############################################
	@ResponseBody
	@RequestMapping(value="/address_getAddressTree")
	public JSONArray getAddressTree(HttpServletRequest request) {
		System.out.println(JSONArray.fromObject(addressService.getAddressList(request)));
		return JSONArray.fromObject(addressService.getAddressList(request));
	}
//###################################################################
	// 进入地址管理页
	@RequestMapping(value="/address_addressManagerList")
	public String enterAddressManagerList(HttpServletRequest request) {
		return "manager/address/addressManagerList";
	}
	// 进入增加或修改 地址页面
	@RequestMapping(value="/address_enterAddOrUpdateAddress/{parentid}")
	public void enterAddOrUpdateAddress(@PathVariable String parentid,HttpServletRequest request,HttpServletResponse response) {
		// 地区 父ID
		try {
			String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort() + request.getContextPath();
			PrintWriter out = response.getWriter();
			out.print(OMSFileUtils.readFileToString(new File(fileRoot+"addOrUpdateAddress.jsp"), "utf-8").
					replaceAll("\\{parentid\\}", parentid).replaceAll("\\{base\\}", basePath));
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	// 进入增加或修改 地址页面
	@ResponseBody
	@RequestMapping(value="/address_saveOrUpdateAddress")
	public Map<String, Object> saveOrUpdateAddress(Address address,HttpServletRequest request) {
		map.clear();
		if(addressService.addAddress(address)){
			map.put("parentid", address.getParentid());
			map.put("newid", address.getId());
		}
		return map;
	}
	//打开单选树页面
	@RequestMapping(value="/address_enterAddressTreeRD")
	public void enterAddressTreeRD(HttpServletRequest request,HttpServletResponse response) {
		try {
			String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort() + request.getContextPath();
			PrintWriter out = response.getWriter();
			out.print(OMSFileUtils.readFileToString(new File(fileRoot+"addressTreeRD.jsp"), "utf-8").
					replaceAll("\\{base\\}", basePath));
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	// 保存 用户/商家与 地主关系信息
	@ResponseBody
	@RequestMapping(value="/address_saveAddressRelation")
	public Map<String, Object> address_saveAddressRelation(HttpServletRequest request) {
		map.clear();
		map.put("id", new Integer(SequenceManager.nextID("address_relation")));
		map.put("type", request.getParameter("type"));
		map.put("mainid", request.getParameter("mainid"));
		map.put("addressid", request.getParameter("addressid"));
		map.put("placename", request.getParameter("placename"));
		map.put("addressdetail", request.getParameter("addressdetail"));
		
		addressService.saveAddressRelation(map);
		return map;
	}
	
	@ResponseBody
	@RequestMapping(value="/address_getRelatedAddressList/{parentId}")
	public Map<String, Object> address_getAddressByParentId(@PathVariable String parentId) {
		map.clear();
		List<Address> list = addressService.getAddressByParentId(parentId);
		map.put("addressList", list);
		return map;
	}
}